﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;

namespace Remodel
{
    public partial class observationlog : Form
    {
        WebBrowser edds;
        public DataTable tridenttable = new DataTable();
        DataRow trow;
        public static grid newrecords;
        int rowcount;
        private string sid;
        private string dl;
        WebBrowser observation = new WebBrowser();
        DateTime today = DateTime.Today;
        DateTime threshold;
        DateTime constrstart;
        string str;
        string seq;
        DateTime csd;
        string strseq;
        bool firstpass = false;
        public static int tr;
        public static Excel.Worksheet spockws;
        int lastrow;

        public observationlog(Excel.Worksheet wsc, WebBrowser eddswebpage)
        {
            edds = eddswebpage;
            spockws = wsc;
            InitializeComponent();
            openobservation();
        }

        private void openobservation()
        {
            HtmlElement table = edds.Document.GetElementById("UserIDTable");
            HtmlElementCollection devlog = table.GetElementsByTagName("td");
            string devlogid = devlog[7].InnerHtml;
            int a = devlogid.IndexOf("SID=");
            int b = devlogid.IndexOf("');");
            sid = devlogid.Substring(a + 4, b - (a + 4));
            HtmlElement devlogin = edds.Document.GetElementById(sid);
            dl = "https://www.bldgportal.com/DeviationLog/LoginDirect.aspx?SID=" + sid;
            observation.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(observation_DocumentCompleted);
            observation.AllowNavigation = true;
            observation.Navigate(dl, false);
        }

        void observation_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (firstpass == false)
            {
                firstpass = true;
                lastrow = spockws.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;

                threshold = today.AddDays(-730);
                

                for (tr = 2; tr < lastrow; tr++)
                {
                    str = Convert.ToString(spockws.Range["B" + tr].Value);
                    seq = Convert.ToString(spockws.Range["C" + tr].Value);

                    if (spockws.Range["AH" + tr].Value is DateTime)
                    {
                        csd = Convert.ToDateTime(spockws.Range["AH" + tr].Value);

                        if (csd > threshold && csd < today.AddDays(-30))
                        {
                            newpage(str, seq);
                        }
                    }
                }
            }

            else if (firstpass == true)
            {
                this.Close();
            }
        }

        public static void newpage(string strs, string seqs)
        {
            
            newrecords = new grid(spockws, tr, strs, seqs);
            newrecords.ShowDialog();
        }
    }
}
